<?php
namespace app\api\controller;

use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_classification_list",
        "recommend_service"
        
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        $list = Db::name("banner")->where("type",$param["type"])->order('id desc')
            ->paginate($num);
        $this->success('获取成功', $list);
    }
 
   
    //分类列表
    public function get_classification_list(){
        $list = Db::name("classification")->order('sort asc')->field("id,title")->paginate(10);
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }

    //分类品牌
    public function cllass_goods(){
        $param = $this->request->param();
        $where = [];
        if(!empty($param['title']) && isset($param['title']))
        {
            $where = [
                ['title', 'like', '%'.$param['title'].'%']
            ];
        }
        if(empty($param["cid"])){
            $list = Db::name("service_goods")->where($where)->select();

        }else{
            $list = Db::name("service_goods")->where($where)->where("service_class",$param["cid"])->select();
        }
        foreach($list as $k=>$v){
            $v["numberof_participants"] = Db::name("my_delegation")->where("gid",$v["id"])->where("is_cancel",0)->count();

            $list[$k] = $v;
        }

        $this->success('获取成功', $list);
    }

    //品牌详情
    public function brand_detail(){
        $param = $this->request->param();
        if(empty($param["gid"])){
            $this->error("未找到指定参数");
        }
        $list = Db::name("service_goods")->where("id",$param["gid"])->find();
        $list["lun_img"] = explode("|",$list["lun_img"]);
        $list['desc'] = html_entity_decode($list['desc']);
        $list['desc'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $list['desc']);
       
        $this->success('获取成功', $list);

    }

    //楼号
    public function user_building_number(){
        $list = Db::name("user_building_number")->order("id desc")->select();
        $this->success('获取成功', $list);
    }
    //户型面积
    public function user_unitarea(){
        $list = Db::name("user_unitarea")->order("id desc")->select();
        foreach($list as $k=>$v){
            $v["unit_type_area"] = $v["unit_type"].'/'.$v["area"].'m²';
            $list[$k] = $v;
        }
        $this->success('获取成功', $list);
    }

    //设置参团信息
    public function set_attendance_information(){
        $param = $this->request->param();
        if(empty($param["avatar"])){
            $this->error("未找到指定参数");
        }
        if(empty($param["nickname"])){
            $this->error("未找到指定参数");
        }
        if(empty($param["phone"])){
            $this->error("未找到指定参数");
        }
        if(empty($param["unitarea_id"])){
            $this->error("未找到指定参数");
        }
        if(empty($param["building_number_id"])){
            $this->error("未找到指定参数");
        }
        Db::name("user")->where("id",$this->uid)->update($param);
        $this->success('设置成功');
    }

    // //参团信息编辑显示
    // public function editattendance_infolook(){
    //     $param = $this->request->param();
    //     if(empty($param["tid"])){
    //         $this->error("未找到指定参数");
    //     }
    //     $list = Db::name("participa_info")->where("id",$param["tid"])->find();
    //     $this->success('获取成功', $list);
    // }

    //一键参团
    public function one_click_participation(){
        $param = $this->request->param();
        if(empty($param["gid"])){
            $this->error("未找到指定品牌");
        }
        //查询用户是否设置了参团信息
        $user = Db::name("user")->where("id",$this->uid)->find();
        if(empty($user["unitarea_id"]) || empty($user["building_number_id"])){
            $this->error("请设置参团信息");
        }
        $flag = Db::name("my_delegation")->where("uid",$this->uid)->where("gid",$param["gid"])->where("is_cancel",0)->find();
        if(!empty($flag)){
            $this->error("该团已参加");
        }
        Db::name("my_delegation")->insert(["uid"=>$this->uid,"gid"=>$param["gid"],"create_time"=>time()]);
        $this->success('提交成功');
    }

    //取消参团
    public function cancel_participation(){
        $param = $this->request->param();
        if(empty($param["gid"])){
            $this->error("未找到指定品牌");
        }
        $flag = Db::name("my_delegation")->where("uid",$this->uid)->where("is_cancel",0)->where("gid",$param["gid"])->find();
        if(empty($flag)){
            $this->error("未参团不能取消");
        }
        Db::name("my_delegation")->where("uid",$this->uid)->where("is_cancel",0)->where("gid",$param["gid"])->update(["is_cancel"=>1]);
        $this->success('取消成功');
    }

    //我的参团列表
    public function list_ofattendees(){
        $list = Db::name("my_delegation")->where("uid",$this->uid)->where("is_cancel",0)
        ->paginate(10)->each(function($item){
            $goods = Db::name("service_goods")->where("id",$item["gid"])->Find();
            $item["service_good_name"] = $goods["title"];
            $item["service_good_zhu_img"] = $goods["zhu_img"];
            $item["create_time"] = date("Y-m-d H:i:s",$item["create_time"]);
            return $item;
        });
        $this->success('获取成功', $list);
    }

    //判断用户是否参团
    public function is_participatein_thedelegation(){
        $param = $this->request->param();
        if(empty($param["gid"])){
            $this->error("未找到指定品牌");
        }
        $flag = Db::name("my_delegation")->where("uid",$this->uid)->where("gid",$param["gid"])->find();
        if(empty($flag)){
            $this->success('未参团可以参团');
        }elseif($flag["is_cancel"] == 1){
            $this->success('参团已取消,可以参团');
        }else{
            $this->error("已参团");
        }
    }

    //首页信 <br/>
    public function shouyexin(){
        $service_agreement = sysconfig('contact','service_agreement');
        $list["service_agreement"] = $service_agreement;
        $list['service_agreement'] = html_entity_decode($list['service_agreement']);
        $list['service_agreement'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '', $list['service_agreement']);
        $this->success('获取成功', $list);
    }






    

    
 
   
}
